package cn.edu.njtc.service.impl;

import cn.edu.njtc.common.ErrorMessage;
import cn.edu.njtc.entity.dto.DoctorLoginDto;
import cn.edu.njtc.entity.dto.DoctorRegisterDto;
import cn.edu.njtc.entity.dto.DoctorTokenDto;
import cn.edu.njtc.entity.pojo.Doctor;
import cn.edu.njtc.entity.pojo.DoctorDetail;
import cn.edu.njtc.exception.BizException;
import cn.edu.njtc.mapper.DoctorMapper;
import cn.edu.njtc.service.DoctorDetailService;
import cn.edu.njtc.service.DoctorService;
import cn.edu.njtc.util.JWTUtil;
import cn.edu.njtc.util.MD5Utils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
 * 医生服务实现类
 */
@Service
@Transactional
public class DoctorServiceImpl extends ServiceImpl<DoctorMapper, Doctor> implements DoctorService {

    @Autowired
    private DoctorMapper doctorMapper;

    @Autowired
    private DoctorDetailService doctorDetailService;

    @Override
    public boolean saveOrUpdate(Doctor doctor) {
        if (StringUtils.hasText(doctor.getPassword())) {
            doctor.setPassword(MD5Utils.md5(doctor.getPassword()));
        }
        return super.saveOrUpdate(doctor);
    }

    @Override
    @Transactional
    public boolean saveDoctorAndDetail(Doctor doctor, DoctorDetail doctorDetail) {
        if (StringUtils.hasText(doctor.getPassword())) {
            doctor.setPassword(MD5Utils.md5(doctor.getPassword()));
        }

        // 1. 保存医生基本信息
        boolean doctorSaved = this.save(doctor);

        if (doctorSaved) {
            // 2. 将医生ID设置到详细信息中
            doctorDetail.setDoctorId(doctor.getId());
            // 3. 保存医生详细信息
            boolean detailSaved = doctorDetailService.save(doctorDetail);
            return detailSaved;
        }
        return false;
    }

    @Override
    public Doctor getDoctorWithDetail(Integer doctorId) {
        // 1. 获取医生基本信息
        Doctor doctor = this.getById(doctorId);

        if (doctor != null) {
            // 2. 获取医生详细信息
            DoctorDetail doctorDetail = doctorDetailService.getById(doctorId);
            System.out.println("查询到医生详细信息：" + doctorDetail);
        }
        return doctor;
    }

    @Override
    @Transactional
    public boolean updateDoctorAndDetail(Doctor doctor, DoctorDetail doctorDetail) {
        // 在更新医生基本信息之前，检查密码是否被修改，如果修改了则加密
        if (StringUtils.hasText(doctor.getPassword())) {
            // 只有当密码字段不为空时才进行加密和更新，这意味着前端传了新密码
            doctor.setPassword(MD5Utils.md5(doctor.getPassword()));
        } else {
            // 如果密码为空，则不更新密码字段
        }


        // 1. 更新医生基本信息
        boolean doctorUpdated = this.updateById(doctor);

        // 2. 更新医生详细信息
        doctorDetail.setDoctorId(doctor.getId());
        boolean detailUpdated = doctorDetailService.updateById(doctorDetail);

        return doctorUpdated && detailUpdated;
    }

    @Override
    @Transactional
    public boolean deleteDoctorAndDetail(Integer doctorId) {
        // 1. 逻辑删除医生基本信息
        Doctor doctor = new Doctor();
        doctor.setId(doctorId);
        doctor.setDeleted(true);
        boolean doctorDeleted = this.updateById(doctor);

        // 2. 逻辑删除医生详细信息
        DoctorDetail doctorDetail = new DoctorDetail();
        doctorDetail.setDoctorId(doctorId);
        doctorDetail.setDeleted(true);
        boolean detailDeleted = doctorDetailService.updateById(doctorDetail);

        return doctorDeleted && detailDeleted;
    }
    @Override
    public int register(DoctorRegisterDto doctorRegisterDto) throws BizException {
        if (doctorRegisterDto.getPhone() == null || doctorRegisterDto.getPhone().length() != 11) {
            throw new BizException(ErrorMessage.INVALID_PHONE_LENGTH);
        }

        Doctor doctor = doctorMapper.selectByPhone(doctorRegisterDto.getPhone());
        if (doctor != null) {
            throw new BizException(ErrorMessage.DUPLICATE_PHONE);
        }
        if (doctorRegisterDto.getPassword() != null && doctorRegisterDto.getPassword().length() > 0) {
            doctorRegisterDto.setPassword(MD5Utils.md5Upper(doctorRegisterDto.getPassword()));
        }
        doctor = new Doctor();
        BeanUtils.copyProperties(doctorRegisterDto, doctor);
        return doctorMapper.insert(doctor);
    }
    @Override
    public DoctorTokenDto login(DoctorLoginDto doctorLoginDto) throws BizException {
        doctorLoginDto.setPassword(MD5Utils.md5Upper(doctorLoginDto.getPassword()));
        DoctorTokenDto doctorTokenDto = doctorMapper.selectByPasswordAndPhone(doctorLoginDto);
        if (doctorTokenDto != null) {
            doctorTokenDto.setToken(JWTUtil.createJWT(doctorTokenDto.getPhone()));
            return doctorTokenDto;
        }
        throw new BizException(ErrorMessage.LOGIN_FAILED);
    }
}